<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/11/5
 * Time: 16:11
 */

namespace Xiaoqu\Controller;
use Common\Controller\HomebaseController;
use Label\Model\CategoryModel;

class XiaoquController extends HomebaseController {
    protected $page_size;
    function _initialize() {
        parent::_initialize();
        $this->page_size = 20;
    }

    // 小区列表
    public function index()
    {
        $areas = getArea(3);
        $this->assign('area', json_encode($areas));
        $this->_lists($areas);
        $this->display();
    }

    public function xiaoqu_info()
    {
        $id = I('id');
        $info = M('xiaoqu')
            ->alias('x')
            ->where(['x.id' => $id])
            ->field('x.*, (select GROUP_CONCAT(name) FROM rx_category where FIND_IN_SET(id, x.categorys) order by id) as category_names')
            ->find();
        $other_info = explode('|', $info['other_info']);
        $temp       = array();
        foreach ($other_info as $v) {
            $sub_info = explode('：', $v);
            if ($sub_info[0]) {
                $temp[$sub_info[0]] = $sub_info[1];
            }
        }
        $info['other_info'] = $temp;
        unset($info['other_info']);
        $this->assign('info', $info);
        $this->display();
    }

    public function _lists($areas)
    {
        $where = array();
        // 分类
        $category_id = I('request.category_id');
        if(!empty($category_id)){
            $where[] = "FIND_IN_SET(" . $category_id . ", categorys)";
        }
        //项目分类
        $categorys = M('category')->where(['type' => CategoryModel::CATEGORY_TYPE_XIAOQU, 'status' => 1])->select();
        $this->assign('categorys', $categorys);

        // 城市
        $city_id = I('request.city_id');
        if(!empty($city_id)){
            $all_city_id = getAreaParentPath($city_id, $areas);
            $where[] = "FIND_IN_SET(city_id, '" . implode(',', $all_city_id) . "')";

            $this->assign('path', json_encode($all_city_id));
        }

        // 年份
        $year_id    = I('request.year');
        $year       = array('', 'build_time <= 1995', 'build_time > 1995 and build_time <= 2000', 'build_time > 2000 and build_time <= 2005', 'build_time > 2005 and build_time <= 2010', 'build_time > 2010');
        if(!empty($year_id) && isset($year[$year_id])){
            $where[] = $year[$year_id];
        }

        $model  = M('xiaoqu');
        $count  = $model->where($where)->count();
        $page   = $this->page($count, $this->page_size);
        $lists  = $model->where($where)
            ->order('build_time desc')
            ->limit($page->firstRow , $page->listRows)
            ->field('*, (select GROUP_CONCAT(name) FROM rx_category where FIND_IN_SET(id, rx_xiaoqu.categorys) order by id) as category_names')
            ->select();
        $this->assign("page", $page->show('default'));
        $this->assign("formget", array_merge($_GET,$_POST));
        $this->assign("lists", $lists);
        $this->assign('total_pages', ceil($count / $this->page_size));
    }

    public function set_xiaoqu_city_id()
    {
        ignore_user_abort(true);
        set_time_limit(0);

        ob_end_clean();
        header("Connection close");
        header("HTTP/1.1 200 OK");

        ob_start();
        echo "执行中！♡♥♡ ~ 你最美 ~ ♡♥♡";
        $size = ob_get_length();
        header("Content-Length: $size");
        ob_end_flush();//输出当前缓冲
        flush();//输出PHP缓冲

        $model  = M('xiaoqu');
        $sql    = "update `rx_xiaoqu` x set x.city_id = (SELECT a.id as city_id from `rx_area` a where a.areaName LIKE CONCAT('%', x.district, '%') ORDER BY a.id asc LIMIT 1) WHERE x.city_id = 0 OR ISNULL(x.city_id)";
        $model->query($sql);
    }
}
